package com.jboost.modules.app.service.strategry.dp;


import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.jboost.common.util.DateUtils;
import com.jboost.common.util.HttpRestUtils;
import com.jboost.common.util.JSONUtil;
import com.jboost.modules.app.constant.BaseResult;
import com.jboost.modules.app.dto.resp.BdDy;
import com.jboost.modules.app.dto.resp.BdSJ;
import com.jboost.modules.app.dto.resp.DpDIanPuRep;
import com.jboost.modules.app.service.strategry.common.AbstractJdy;
import com.jboost.modules.system.entity.*;
import com.jboost.modules.system.mapper.DpDianpuMapper;
import com.jboost.modules.system.mapper.PdmPlMapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.boot.ApplicationArguments;
import org.springframework.boot.ApplicationRunner;
import org.springframework.http.HttpHeaders;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Service;

import java.io.IOException;
import java.util.*;
import java.util.stream.Collectors;

import static com.jboost.common.util.DateUtils.yyyyMMdd;

@Service(value = "DpDianpuQuery")
@Slf4j
public class DpDianpuQuery extends AbstractJdy<String, DpDianpu, DpDianpuMapper, DpDianpu>  {


    public String getFormId() {
        return "63f0eafcaa557b0008d09c9c";
    }


    public String getFormId1() {
        return "63f0bcda3d300400077367af";
    }

    public HttpHeaders getJdyHeaders(){
        HttpHeaders jdyHeaders = new HttpHeaders();
        jdyHeaders.set("Authorization","Bearer "+ "bdfDElitwqhraWZoCv3uxdv9PysCV605");
        return jdyHeaders;
    }

    public String getAppId() {
        return "64880dc1f3ecc50008fbae61";
    }



    @Scheduled(cron = "0 0/5 * * * ?")
    public BaseResult<Boolean> invokeStrategy() throws Exception {
        DpDianpu hxProduct = new DpDianpu();
        sync(hxProduct);
        sync1();
        return BaseResult.success();
    }


    public void sync1() throws IOException {
        log.info("开始批量查询Hs材料数据");
        //baseMapper.delete((Wrapper<PdmPl>) Wrappers.lambdaQuery(entit.getClass()).orderByDesc(BaseEntity::getId).ne(BaseEntity::getId,"0"));
        HashMap<String, Object> param = getParam();
        param.put("entry_id",getFormId1());
        param.put("limit",1000);
        List<JSONObject> jsonObjects = new ArrayList<>();
        do {
            String post = HttpRestUtils.post(query_all_api, param, getJdyHeaders());
            jsonObjects   = (List<JSONObject>) JSONUtil.fromJson(post, Map.class).get("data");
            jsonObjects.forEach(e1 -> {
                try {
                    DpDIanPuRep.DataBean bdDy = JSONUtil.fromJson(e1.toJSONString(), DpDIanPuRep.DataBean.class);
                    String pt = bdDy.getPlatform_id();
                    String dpmc = bdDy.getShop_name();
                    String date = bdDy.getUpdateTime();
                    Date parse = yyyyMMdd.get().parse(date);
                    Date date1 = DateUtils.addDays(parse, 90);
                    List<DpDianpu> pdmBoms = baseMapper.selectList(Wrappers.lambdaQuery(DpDianpu.class).eq(DpDianpu::getPt, pt).eq(DpDianpu::getDpmc, dpmc).le(DpDianpu::getTime, date1));
                    if (!CollectionUtils.isEmpty(pdmBoms)){
                        DpDianpu dpDianpu = pdmBoms.get(0);
                        DpDIanPuRep.DataBeanReq dataBeanReq = new DpDIanPuRep.DataBeanReq();
                        dataBeanReq.set_widget_1676721370572(getParaMap(dpDianpu.getKj()));
                        dataBeanReq.set_widget_1676904957575(getParaMap(dpDianpu.getB2b()));
                        dataBeanReq.set_widget_1702874603417(getParaMap(dpDianpu.getYy()));
                        jdyUpdate1(bdDy.get_id(),dataBeanReq);
                    }


                }catch (Exception exception){

                }

            });
            if (jsonObjects.size()>0){

                param.put("data_id",jsonObjects.get(jsonObjects.size()-1).getString("_id"));
            }
        }while (jsonObjects.size()>0);

    }

    public void sync(DpDianpu entit) throws IOException {
        log.info("开始批量查询Hs材料数据");
        baseMapper.delete(Wrappers.lambdaQuery(DpDianpu.class).orderByDesc(BaseEntity::getId).ne(BaseEntity::getId,"0"));
        HashMap<String, Object> param = getParam();
        param.put("limit",1000);
        List<JSONObject> jsonObjects = new ArrayList<>();
        do {
            String post = HttpRestUtils.post(query_all_api, param, getJdyHeaders());
            jsonObjects   = (List<JSONObject>) JSONUtil.fromJson(post, Map.class).get("data");
            jsonObjects.forEach(e -> {
                try {
                    DpDianpu dpDingdan = JSONObject.parseObject(e.toString(), entit.getClass());
                    Object o = baseMapper.selectById(e.getString("_id"));
                    dpDingdan.setTime(sdf1.parse(e.getString("updateTime")));
                    try {
                        dpDingdan.setYy(e.getJSONObject("yy").getString("username"));
                    }catch (Exception exception){

                    }
                    if (Objects.isNull(o)) {
                        baseMapper.insert(dpDingdan);
                    }else if (!Objects.equals(o,dpDingdan)) {
                        baseMapper.deleteById(e.getString("_id"));
                        baseMapper.insert(dpDingdan);
                    }
                }catch (Exception exception){

                }

            });
            if (jsonObjects.size()>0){

                param.put("data_id",jsonObjects.get(jsonObjects.size()-1).getString("_id"));
            }
        }while (jsonObjects.size()>0);

    }


    @Override
    public void run(ApplicationArguments args) throws Exception {

    }
}
